package interfaz;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.Timer;

public class DialogoCaptura extends JDialog implements ActionListener{
	
	private static final String PARAR = "PARAR";
	
	private int width = 400;
	
	private int height = 150;
	
	private Timer timer;
	private Font font;
	
	private JLabel labelCronometro;
	private JButton botonParar;
	private SimpleDateFormat simpleDateFormat;
	private InterfazTMS interfazTMS;
	private long startTime;
	
	
	public DialogoCaptura(InterfazTMS interfaz)
	{
		interfazTMS = interfaz;
		font = new Font("Verdana", Font.BOLD, 60);
		setLayout(new GridLayout(2, 1));
		setSize(width, height);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setResizable(false);
		
		labelCronometro = new JLabel();
		labelCronometro.setFont(font);
		labelCronometro.setHorizontalAlignment(JLabel.CENTER);
		labelCronometro.setText("00:00");
		botonParar = new JButton("Parar");
		botonParar.setActionCommand(PARAR);
		botonParar.addActionListener(this);
		
		simpleDateFormat = new SimpleDateFormat("mm:ss");
		
		add(labelCronometro);
		add(botonParar);
	}
	
	public void setPosition(Dimension screenSize)
	{
		setLocation((screenSize.width/2) - (width/2), (screenSize.height/2) - (height/2));
		timer = new Timer(1000, this);
	}
	
	public void iniciar()
	{
		timer.start();
		startTime = System.currentTimeMillis();
		labelCronometro.setText("00:00");
	}

	public void setPaciente(int paciente)
	{
		setTitle("Paciente " + paciente);
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		long transcurrido = System.currentTimeMillis() - startTime;
		if(event.getSource() instanceof Timer)
		{
			labelCronometro.setText(simpleDateFormat.format(new Date(transcurrido)));
		}
		else
		{
			interfazTMS.terminarCaptura(transcurrido);
		}
	}
	
	public void parar()
	{
		timer.stop();
	}
	
}
